package com.zwj.batch.config.test;

import com.zwj.batch.config.springBatch.listener.job.JobAnnoStateListener;
import com.zwj.batch.config.springBatch.listener.job.JobStateListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.listener.JobListenerFactoryBean;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.JobContext;
import org.springframework.batch.core.scope.context.JobSynchronizationManager;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 张文接
 * @version 1.0
 * @createTime 2023-05-24 21:15:42
 */
//@Configuration
public class BatchConfig06 {
    //job构造工厂
    @Resource
    private JobBuilderFactory jobBuilderFactory;
    //step构造工厂
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 逻辑主体
     *
     * @return
     */
    @StepScope
    @Bean
    public Tasklet tasklet() {
        return (stepContribution, chunkContext) -> {
            Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
            System.out.println(jobParameters.get("name") + "=======" + jobParameters.get("daily"));

            JobExecution jobExecution = stepContribution.getStepExecution().getJobExecution();
            System.err.println("执行中-status：" + jobExecution.getStatus());
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("status-anno-listener-job")//给job命名
                .start(step())//具体执行什么步骤(step)
                .listener(JobListenerFactoryBean.getListener(new JobAnnoStateListener()))
//                .listener(jobStateListener())//监听器
                .build();
    }

    @Bean
    public Step step() {
        return stepBuilderFactory
                .get("step")//给step命名
                .tasklet(tasklet())//注入具体要执行的逻辑
                .build();//构建
    }

    //监听器
    @Bean
    public JobStateListener jobStateListener() {
        return new JobStateListener();
    }

    /**
     * 作业上下文API
     */
    private void jobApi() {
        JobContext jobContext = JobSynchronizationManager.getContext();//作业上下文
        JobExecution jobExecution = jobContext.getJobExecution();//作业执行器
        Map<String, Object> parameters = jobContext.getJobParameters();//作业入参
        Map<String, Object> jobExecutionContext = jobContext.getJobExecutionContext();//作业执行器上下文
    }

    /**
     * 步骤上下文API
     */
    public void stepApi() {
        ChunkContext chunkContext = null;
        StepContext stepContext = chunkContext.getStepContext();
        StepExecution stepExecution = stepContext.getStepExecution();
        Map<String, Object> stepExecutionContext = stepContext.getStepExecutionContext();
        Map<String, Object> jobExecutionContext = stepContext.getJobExecutionContext();
    }

    /**
     * 执行器上下文API
     */
    public void executionApi() {
        ChunkContext chunkContext = null;

        //步骤执行期上下文额
        StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
        ExecutionContext executionContext = stepExecution.getExecutionContext();
        executionContext.put("key", "value");

        //作业执行器上下文
        ExecutionContext jobExecutionContext = stepExecution.getJobExecution().getExecutionContext();
        jobExecutionContext.put("key", "value");
    }
}
